package com.rf.richfitwheel.admin.sys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.rf.richfitwheel.common.contants.CommonServiceError;
import com.rf.richfitwheel.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.rf.richfitwheel.admin.annotation.UserOperateLog;
import com.rf.richfitwheel.admin.sys.model.Dictionary;
import com.rf.richfitwheel.admin.sys.service.SysDicService;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;
import com.rf.richfitwheel.common.utils.TreeUtils;
import com.rf.richfitwheel.admin.contants.UrlConstant;

/**
 * 字典管理
 *
 * @author hushouquan
 * @date 2018-12-12
 */
@RestController
@RequestMapping(UrlConstant.WEB_ADMIN_URL + "/dic")
public class SysDicController {

    private static Logger log = LoggerFactory.getLogger(SysDicController.class);

    @Autowired
    private SysDicService sysDicService;

    /**
     * 所有配置列表
     */
    @GetMapping("/list")
    public R list(String page, String limit, String dataForm) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("limit", limit);
        params.put("dataForm", dataForm);
        PageUtils pages = sysDicService.queryPage(params);
        return R.ok().put("page", pages);
    }

    /**
     * 根据字典编号取字典数据
     *
     * @param classCode
     * @return
     */
    @GetMapping("/getDiclist/{classCode}")
    public R getDiclist(@PathVariable("classCode") String classCode) {
        List<Dictionary> list = sysDicService.queryByClassCode(classCode);
        return R.ok().put("list", list);
    }

    /**
     * 根据字典编号取字典数据
     *
     * @param classCode
     * @return
     */
    @GetMapping("/getDicByClassCodeAndDataCode/{classCode}/{dataCode}")
    public R getDicByClassCodeAndDataCode(@PathVariable("classCode") String classCode, @PathVariable("dataCode") String dataCode) {
        Dictionary dictionary = sysDicService.queryByClassCodeAndDataCode(classCode, dataCode);
        return R.ok().put("dictionary", dictionary);
    }

    @GetMapping("/getDiclistAll")
    public R getDiclistAll() {
        List<Dictionary> list = sysDicService.queryAll();
        return R.ok().put("list", list);
    }

    /**
     * 配置信息
     *
     * @throws Exception
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) throws Exception {
        Dictionary dictionary = sysDicService.selectById(id);
        return R.ok().put("dic", dictionary);
    }

    /**
     * 保存配置
     */
    @UserOperateLog(module = "系统功能", methods = "save", description = "新增数据字典")
    @PostMapping("/save")
    public R save(@RequestBody Dictionary dictionary) {
        try {
            sysDicService.save(dictionary);
            return R.ok();
        }  catch (BusinessException e){
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error(CommonServiceError.SERVICE_SAVE.getMsg() + "数据字典" + CommonServiceError.SERVICE_FAILURE.getMsg(), e);
            return R.error(CommonServiceError.SERVICE_SAVE.getMsg() + "数据字典" + CommonServiceError.SERVICE_FAILURE.getMsg());
        }
    }

    /**
     * 修改配置
     */
    @UserOperateLog(module = "数据字典管理", methods = "update", description = "修改数据字典")
    @PostMapping("/update")
    public R update(@RequestBody Dictionary dictionary) {
        try {
            sysDicService.update(dictionary);
            return R.ok();
        }  catch (BusinessException e){
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error(CommonServiceError.SERVICE_MODIFY.getMsg() + "数据字典" + CommonServiceError.SERVICE_FAILURE.getMsg(), e);
            return R.error(CommonServiceError.SERVICE_MODIFY.getMsg() + "数据字典" + CommonServiceError.SERVICE_FAILURE.getMsg());
        }
    }

    /**
     * 删除配置
     */
    @UserOperateLog(module = "系统功能", methods = "delete", description = "删除数据字典")
    @PostMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        sysDicService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * <p>Title: 根据字典代码加载数据字典树结构</p>
     * <p>Description: 根据字典代码加载数据字典树结构</p>
     *
     * @param classCode 字典代码,多个用逗号隔开
     * @return
     */
    @GetMapping("/getDicTreeByClassCode/{classCode}")
    public R getDicTreeByClassCode(@PathVariable("classCode") String classCode) {
        try {
            List<TreeUtils> list = sysDicService.getDicTreeByClassCode(classCode);
            return R.ok().put("dicTree", list);
        } catch (Exception e) {
            log.error("根据字典代码加载数据字典树结构失败!", e);
            e.printStackTrace();
            return R.error(1, "根据字典代码加载数据字典树结构失败!");
        }
    }
}
